package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.ams.domain.vo.AssetBrandAddVo;
import com.ruoyi.ams.domain.vo.AssetBrandEditVo;
import com.ruoyi.ams.domain.vo.AssetBrandListVo;
import com.ruoyi.ams.domain.vo.KeyValueVo;
import com.ruoyi.ams.service.IAmsAssetBrandService;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.AmsAssetBrand;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.asms.service.IAsmsAssetFullInfoService;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 品牌信息Controller
 *
 * @author yfkj
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/ams/AmsAssetBrand")
public class AmsAssetBrandController extends BaseController {
    @Autowired
    private IAmsAssetBrandService amsAssetBrandService;

    @Autowired
    private IAsmsAssetFullInfoService assetFullInfoService;

    /**
     * 查询品牌信息列表
     */
   // @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:list")
    @GetMapping("/list")
    @ApiOperation("查询品牌信息列表")
    public TableDataInfo list(AssetBrandListVo vo) {
        startPage();
        AmsAssetBrand amsAssetBrand = new AmsAssetBrand();
        BeanUtils.copyProperties(vo, amsAssetBrand);
        List<AmsAssetBrand> list = amsAssetBrandService.selectAmsAssetBrandList(amsAssetBrand);
        return getDataTable(list);
    }

    /**
     * 下拉选择列表
     */
    @ApiOperation("品牌下拉列表")
    @GetMapping("/selectList")
    public AjaxResult list() {
        List<AmsAssetBrand> list = amsAssetBrandService.list(new QueryWrapper<AmsAssetBrand>());
        List<KeyValueVo> vos = new ArrayList<>();
        for (AmsAssetBrand brand : list) {
            KeyValueVo vo = new KeyValueVo();
            vo.setKey(brand.getBrandId());
            vo.setValue(brand.getBrandName());
            vos.add(vo);
        }
        return AjaxResult.success(vos);
    }

    /**
     * 导出品牌信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:export")
    @Log(title = "品牌信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    @ApiOperation("导出品牌信息列表")
    public void export(HttpServletResponse response, AmsAssetBrand amsAssetBrand) throws IOException {
        List<AmsAssetBrand> list = amsAssetBrandService.selectAmsAssetBrandList(amsAssetBrand);
        ExcelUtil<AmsAssetBrand> util = new ExcelUtil<AmsAssetBrand>(AmsAssetBrand.class);
        util.exportExcel(response, list, "品牌信息数据");
    }

    /**
     * 获取品牌信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:query")
    @GetMapping(value = "/{brandId}")
    @ApiOperation("获取品牌信息详细信息, brandId-品牌ID-Long-必填")
    public AjaxResult getInfo(@PathVariable("brandId") Long brandId) {
        return AjaxResult.success(amsAssetBrandService.selectAmsAssetBrandByBrandId(brandId));
    }

    /**
     * 新增品牌信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:add")
    @Log(title = "品牌信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增品牌信息")
    public AjaxResult add(@RequestBody AssetBrandAddVo vo) {
        AmsAssetBrand amsAssetBrand = new AmsAssetBrand();
        BeanUtils.copyProperties(vo, amsAssetBrand);
        AmsAssetBrand brand = amsAssetBrandService.getOne(new QueryWrapper<AmsAssetBrand>()
                .eq(AmsAssetBrand.BRAND_NAME, vo.getBrandName()), false);
        AmsAssetBrand assetBrand = amsAssetBrandService.getOne(new QueryWrapper<AmsAssetBrand>()
                .eq(AmsAssetBrand.BRAND_TELEPHONE, vo.getBrandTelephone()), false);
        if (brand != null) {
            return AjaxResult.error(vo.getBrandName() + "已存在！");
        }
        if (assetBrand != null) {
            return AjaxResult.error(vo.getBrandTelephone() + "已存在！");
        }
        if (vo.getBrandTelephone().length() > 15) {
            return AjaxResult.error("联系方式最长为15位！");
        }
        amsAssetBrand.setCreateBy(SecurityUtils.getUsername());
        amsAssetBrand.setCreateTime(System.currentTimeMillis());
        return toAjax(amsAssetBrandService.insertAmsAssetBrand(amsAssetBrand));
    }

    /**
     * 修改品牌信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:edit")
    @Log(title = "品牌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改品牌信息")
    public AjaxResult edit(@RequestBody AssetBrandEditVo vo) {
        AmsAssetBrand amsAssetBrand = new AmsAssetBrand();
        BeanUtils.copyProperties(vo, amsAssetBrand);
        if (vo.getBrandTelephone().length() > 15) {
            return AjaxResult.error("联系方式最长为15位！");
        }
        amsAssetBrand.setUpdateBy(SecurityUtils.getUsername());
        amsAssetBrand.setUpdateTime(System.currentTimeMillis());

        //修改资产视图表中的相关数据
        assetFullInfoService.update(new UpdateWrapper<AsmsAssetFullInfo>()
                .set(AsmsAssetFullInfo.BRAND_NAME,vo.getBrandName())
                .set(AsmsAssetFullInfo.BRAND_TELEPHONE,vo.getBrandTelephone())
                .eq(AsmsAssetFullInfo.BRAND_ID,vo.getBrandId()));


        return toAjax(amsAssetBrandService.updateAmsAssetBrand(amsAssetBrand));
    }

    /**
     * 删除品牌信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetBrand:remove")
    @Log(title = "品牌信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{brandId}")
    @ApiOperation("删除品牌信息,brandId-品牌信息ID-Long-必填")
    public AjaxResult remove(@PathVariable Long brandId) {
        return toAjax(amsAssetBrandService.deleteAmsAssetBrandByBrandId(brandId));
    }

    @GetMapping("/getAssetBrandList")
    public R<List<AmsAssetBrand>> getAssetBrandList() {
        return R.ok(amsAssetBrandService.list());
    }
}
